@extends('layout.master')

@section('title','游戏详情')

@section('css')
    @parent
    <link href="{{ asset('plugins/bower_components/summernote/dist/summernote.css') }}" rel="stylesheet"/>
    <!-- animation CSS -->
    <link href="{{ asset('css/animate.css') }}" rel="stylesheet">
    <link href="{{ asset('plugins/bower_components/icheck/skins/all.css') }}" rel="stylesheet">
    <link href="{{ asset('plugins/bower_components/bootstrap-switch/bootstrap-switch.min.css') }}" rel="stylesheet">
@endsection
@section('main')
    <div class="white-box">
        <form class="form-horizontal form-material" method="post" enctype="multipart/form-data">
            <div class="row">
                <input type="hidden" name="id" id="id" value="{{ $gameStatus->id }}">
                <div class="col-md-3 col-sm-3">
                    <div class="form-group">
                        <label class="col-md-12">状态名</label>
                        <div class="col-md-12">
                            <input type="text" name="name" id="name" value="{{ $gameStatus->name }}"
                                   class="form-control form-control-line" required>
                        </div>
                    </div>
                </div>

                <div class="col-md-3 col-sm-3">
                    <div class="form-group">
                        <label class="col-md-12">最小能量值</label>
                        <div class="col-md-12">
                            <input type="text" name="min_energy" id="min-energy" value="{{ $gameStatus->min_energy }}"
                                   class="form-control form-control-line" required>
                        </div>
                    </div>
                </div>

                <div class="col-md-3 col-sm-3">
                    <div class="form-group">
                        <label class="col-md-12">最大能量值</label>
                        <div class="col-md-12">
                            <input type="text" name="max_energy" id="max-energy"
                                   value="{{ $gameStatus->max_energy }}"
                                   class="form-control form-control-line" required>
                        </div>
                    </div>
                </div>

                <div class="col-md-3 col-sm-3">
                    <div class="form-group">
                        <label class="col-md-12">还需花费XX金币</label>
                        <div class="col-md-12">
                            <input type="text" name="discount" id="discount"
                                   value="{{ $gameStatus->discount }}"
                                   class="form-control form-control-line" required>
                        </div>
                    </div>
                </div>

                <div class="col-md-4 col-sm-4">
                    <label class="col-md-12">状态图</label>
                    <div class="col-md-12">
                        <div class="form-group ">
                            <input type="file" id="img-url" class="dropify" name="img_url"
                                   data-default-file="{{ $gameStatus->img_url }}"
                                   data-height="200px"/>
                            <input type="hidden" name="img" id="img" value="{{ $gameStatus->img_url }}">
                        </div>
                    </div>
                </div>
            </div>

        </form>
        <div class="form-group">
            <button class="fcbtn btn btn-success btn-outline btn-1e waves-effect" id="modify">修改
            </button>
            {!! csrf_field() !!}
            <a class="fcbtn btn btn-info btn-outline btn-1e waves-effect"
               href="{{ url('/game/gameInfo') . '/' . $gameStatus->game_id }}">返回
            </a>
        </div>
    </div>

@endsection
@section('js')
    @parent

    <script>
        $("#modify").click(function (e) {
            e.preventDefault();
            var id = $("#id").val();
            var name = $("#name").val();
            var min_energy = $("#min-energy").val();
            var max_energy = $("#max-energy").val();
            var discount = $("#discount").val();
            var img = document.getElementById("img-url").files;


            swal({
                    title: "",
                    text: "确定修改么？",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "是的",
                    closeOnConfirm: false
                },
                function () {
                    if (img.length) {
                        if ((img[0].type).indexOf("image/") === -1) {
                            swal("请上传图片!");
                            return false;
                        }
                        var img_url = picajax(img[0]);
                    } else {
                        img_url = $("#img").val();
                    }

                    $.ajax({

                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        url: "/game/status/info/" + id,
                        type: "POST",
                        data: {
                            name: name,
                            min_energy: min_energy,
                            max_energy: max_energy,
                            discount: discount,
                            img_url: img_url
                        },
                        error: function (e) {
                            if (e.readyState === 4 && e.status === 422) {
                                for (var i in e.responseJSON) {
                                    if (e.responseJSON[i] !== 'The given data was invalid.') {
                                        err = e.responseJSON[i];
                                        for (var j in err) {
                                            toasterror(err[j][0]);
                                        }
                                    }
                                }
                            }
                        },

                        success: function (data) {
                            if (data.status !== 1) {
                                swal(data.message, '', 'success');
                                $('.confirm').click(function () {
                                    location.reload();
                                });
                            } else {
                                swal(data.message);
                            }
                        }
                    })
                }
            );
        });

    </script>
@endsection